﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace SppdProjectHelper
{
    /// <summary>
    /// 解析插件容器中得插件对象，将其分别放置在不同的集合中
    /// </summary>
    public class ParsePluginCollection
    {
        private static Dictionary<string, IAppModule> _app_modules = new Dictionary<string, IAppModule>();
        private static Dictionary<string, ICommand> _commands = new Dictionary<string, ICommand>();
        private static Dictionary<string, ITool> _tools = new Dictionary<string, ITool>();
        private static Dictionary<string, List<IMenuDef>> _menus_defs = new Dictionary<string, List<IMenuDef>>();
        private static Dictionary<string, List<IToolBarDef>> _tools_defs = new Dictionary<string, List<IToolBarDef>>();

        private static ArrayList _CommandCategorys = new ArrayList();
        private static ArrayList _ToolCategorys = new ArrayList();

        public ParsePluginCollection()
        {
        }

        public static Dictionary<string, IAppModule> AppModules { get { return _app_modules; } }
        public static Dictionary<string, ICommand> Commands { get { return _commands; } }
        public static Dictionary<string, ITool> Tools { get { return _tools; } }
        public static Dictionary<string, List<IMenuDef>> MenusDefs { get { return _menus_defs; } }
        public static Dictionary<string, List<IToolBarDef>> ToolsDefs { get { return _tools_defs; } }

        public static ArrayList CommandCategorys { get { return _CommandCategorys; } }
        public static ArrayList ToolCategorys { get { return _ToolCategorys; } }

        //解析插件集合中所有对象
        //将其分别装入ICommand，ITool，IToolBarDef和IMenuDef 4个集合
        public static void ParseRawPluginArray(List<IPlugin> lst_plugin)
        {
            //初始化所有的集合容器
            _app_modules = new Dictionary<string, IAppModule>();
            _commands = new Dictionary<string, ICommand>();
            _tools = new Dictionary<string, ITool>();
            _menus_defs = new Dictionary<string, List<IMenuDef>>();
            _tools_defs = new Dictionary<string, List<IToolBarDef>>();
            _CommandCategorys = new ArrayList();
            _ToolCategorys = new ArrayList();

            foreach (IPlugin plugin in lst_plugin)
            {
                {
                    IAppModule cmd = plugin as IAppModule;
                    if ((cmd != null)&& (cmd.Name != null))
                    {
                        _app_modules.Add(cmd.ToString(), cmd);
                        //找出该Command的Category，如果它还没有添加到Category中，则添加
                        if (cmd.Category != null && _CommandCategorys.Contains(cmd.Category) == false)
                        {
                            _CommandCategorys.Add(cmd.Category);
                            continue;
                        }
                    }
                }
                {
                    ICommand cmd = plugin as ICommand;
                    if ((cmd != null)&& (cmd.Name != null))
                    {
                        _commands.Add(cmd.Name, cmd);
                        //找出该Command的Category，如果它还没有添加到Category中，则添加
                        if (cmd.Category != null && _CommandCategorys.Contains(cmd.Category) == false)
                        {
                            _CommandCategorys.Add(cmd.Category);
                            continue;
                        }
                    }
                }
                {
                    ITool cmd = plugin as ITool;
                    if ((cmd != null)&& (cmd.Name != null))
                    {
                        _tools.Add(cmd.Name, cmd);
                        //找出该Command的Category，如果它还没有添加到Category中，则添加
                        if (cmd.Category != null && _ToolCategorys.Contains(cmd.Category) == false)
                        {
                            _ToolCategorys.Add(cmd.Category);
                            continue;
                        }
                    }
                }
                {
                    IMenuDef cmd = plugin as IMenuDef;
                    if ((cmd != null)&& (cmd.Name != null))
                    {
                        List<IMenuDef> lst_menu;
                        if (false == _menus_defs.ContainsKey( cmd.Name ) )
                        {/*如果没有此对象则新建一个*/
                            lst_menu = new List<IMenuDef>();
                            _menus_defs.Add(cmd.Name, lst_menu);
                        }
                        lst_menu = _menus_defs[cmd.Name];
                        lst_menu.Add(cmd);
                    }
                }
                {
                    IToolBarDef cmd = plugin as IToolBarDef;
                    if ((cmd != null)&&(cmd.Name!=null))
                    {
                        List<IToolBarDef> lst_tool;
                        if (false == _tools_defs.ContainsKey(cmd.Name))
                        {/*如果没有此对象则新建一个*/
                            lst_tool = new List<IToolBarDef>();
                            _tools_defs.Add(cmd.Name, lst_tool);
                        }
                        lst_tool = _tools_defs[cmd.Name];
                        lst_tool.Add(cmd);
                    }
                }
            }
        }
    }
}
